package com.micro.frog.acct.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.frog.acct.dao.RoleMapper;
import com.micro.frog.acct.dto.RoleDTO;
import com.micro.frog.acct.entity.Dict;
import com.micro.frog.acct.entity.Role;
import com.micro.frog.acct.service.RoleService;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色操作
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Role> all(Role role) {
        return roleMapper.selectByCondition(role);
    }

    @Override
    public PageResult<Role> listPage(RoleDTO pageVO) {
        pageVO.init();
        Role role = new Role();
        BeanUtils.copyProperties(pageVO,role);
        PageResult<Role> pageResult = new PageResult<>();
        Page page = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize(),true);
        List<Role> list = roleMapper.selectByCondition(role);
        pageResult.setCode("10000");
        pageResult.setMsg("success");
        pageResult.setData(list);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public int add(Role role) {
        role.setIsDeleted("N");
        role.setCreatedTime(new Date());
        return roleMapper.insertSelective(role);
    }

    @Override
    public int delete(Integer id) {
        Role role = new Role();
        role.setId(id);
        role.setIsDeleted("Y");
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int update(Role role) {
        role.setUpdatedTime(new Date());
        return roleMapper.updateByPrimaryKeySelective(role);
    }
}
